import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
public class JDBCTest4 {
    //JDBC查询数据
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java100?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");

        //连接
        Connection connection = dataSource.getConnection();

        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //执行sql
        ResultSet resultSet = statement.executeQuery();
        //遍历结果集合
        //先取出结果中的每一行，再取出每一行中需要的列
        //代码很想迭代器遍历.
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(id+"\t"+name);
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
